package com.jadeen.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.jadeen.common.utils.DateUtils;
import com.jadeen.entity.domain.Resource;
import com.jadeen.entity.domain.ResourceLink;
import com.jadeen.entity.domain.ResourceTag;
import com.jadeen.entity.domain.User;
import com.jadeen.enums.PlantTypeEnum;
import com.jadeen.mapper.ResourceMapper;
import com.jadeen.service.IResourceLinkService;
import com.jadeen.service.IResourceService;
import com.jadeen.service.IResourceTagService;
import com.jadeen.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 资源Service业务层处理
 * 
 * @author jadeen
 * @date 2025-08-08
 */
@Service
public class ResourceServiceImpl implements IResourceService
{
    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private IResourceTagService resourceTagService;

    @Autowired
    private IResourceLinkService resourceLinkService;

    @javax.annotation.Resource(name = "userServiceClient")
    private IUserService userService;
    /**
     * 查询资源
     * 
     * @param resourceId 资源主键
     * @return 资源
     */
    @Override
    public Resource selectResourceByResourceId(Long resourceId)
    {
        Resource resource = resourceMapper.selectResourceByResourceId(resourceId);
        // 标签
        ResourceTag resourceTag = new ResourceTag();
        resourceTag.setResourceId(resource.getResourceId());
        List<ResourceTag> resourceTags = resourceTagService.selectResourceTagList(resourceTag);
        List<Long> collect = resourceTags.stream().map(ResourceTag::getTagId).collect(Collectors.toList());
        resource.setTags(collect);
        // 链接
        ResourceLink resourceLink = new ResourceLink();
        resourceLink.setResourceId(resource.getResourceId());
        List<ResourceLink> resourceLinks = resourceLinkService.selectResourceLinkList(resourceLink);
        resource.setResourceLink(new ResourceLink());
        // 用户
        User user = userService.selectUserByUserId(resource.getUserId());
        System.out.println(user);
        if (user != null){

            resource.setAvatar(user.getAvatar());
            resource.setNickname(user.getNickname());
        }
        if (!resourceLinks.isEmpty()){
            resource.setResourceLink(resourceLinks.get(0));
        }
        return resource;
    }

    /**
     * 查询资源列表
     * 
     * @param resource 资源
     * @return 资源
     */
    @Transactional
    @Override
    public List<Resource> selectResourceList(Resource resource)
    {
        List<Resource> resources = resourceMapper.selectResourceList(resource);
        return resources;
    }

    /**
     * 新增资源
     * 
     * @param resource 资源
     * @return 结果
     */
    @Transactional
    @Override
    public int insertResource(Resource resource)
    {
        resource.setCreateTime(DateUtils.getNowDate());
        resource.setUserId(Long.valueOf((String) StpUtil.getLoginId()));
        int flag = resourceMapper.insertResource(resource);
        Long resourceId = resource.getResourceId();
        resource.getResourceLink().setResourceId(resourceId);
        resource.getResourceLink().setPlatform(getPlant(resource.getResourceLink().getLinkUrl()));
        resource.getResourceLink().setPassword(getPassword(resource.getResourceLink().getLinkUrl()));
        resourceLinkService.insertResourceLink(resource.getResourceLink());
        if(resource.getTags()!=null){
            resource.getTags().forEach(item -> {
                ResourceTag resourceTag = new ResourceTag();
                resourceTag.setResourceId(resourceId);
                resourceTag.setTagId(item);
                resourceTagService.insertResourceTag(resourceTag);
            });
        }
        return flag;
    }
    public static String getPlant(String link){
        if (link.contains(PlantTypeEnum.BAIDU.getCode())){
            return PlantTypeEnum.BAIDU.getDesc();
        }else if (link.contains(PlantTypeEnum.KUAKE.getCode())){
            return PlantTypeEnum.KUAKE.getDesc();
        }else if (link.contains(PlantTypeEnum.XUNLEI.getCode())){
            return PlantTypeEnum.XUNLEI.getDesc();
        }else {
            return "";
        }
    }

//    public static void main(String[] args) {
//        System.out.println(getPlant("https://pan.baidu.com/s/1wZY5_XZ?pwd=1234"));
//    }
    public static String getPassword(String link){
        if (link.contains("pwd=")){
            return link.substring(link.indexOf("pwd=")+4);
        }else {
            return "";
        }
    }
    /**
     * 修改资源
     * 
     * @param resource 资源
     * @return 结果
     */
    @Transactional
    @Override
    public int updateResource(Resource resource)
    {
        resource.setUpdateTime(DateUtils.getNowDate());
        if(resource.getResourceLink()!=null){
            resource.getResourceLink().setResourceId(resource.getResourceId());
            resource.getResourceLink().setPlatform(getPlant(resource.getResourceLink().getLinkUrl()));
            resource.getResourceLink().setPassword(getPassword(resource.getResourceLink().getLinkUrl()));
            resourceLinkService.updateResourceLink(resource.getResourceLink());
        }
        if(resource.getTags()!=null){
            resourceTagService.deleteResourceTagByResourceId(resource.getResourceId());
            resource.getTags().forEach(item -> {
                ResourceTag resourceTag = new ResourceTag();
                resourceTag.setResourceId(resource.getResourceId());
                resourceTag.setTagId(item);
                resourceTagService.insertResourceTag(resourceTag);
            });
        }
        return resourceMapper.updateResource(resource);
    }

    /**
     * 批量删除资源
     * 
     * @param resourceIds 需要删除的资源主键
     * @return 结果
     */
    @Override
    public int deleteResourceByResourceIds(Long[] resourceIds)
    {
        return resourceMapper.deleteResourceByResourceIds(resourceIds);
    }

    /**
     * 删除资源信息
     * 
     * @param resourceId 资源主键
     * @return 结果
     */
    @Override
    public int deleteResourceByResourceId(Long resourceId)
    {
        return resourceMapper.deleteResourceByResourceId(resourceId);
    }

    @Override
    public List<Resource> selectDownLoadAndView() {
        return resourceMapper.selectDownLoadAndView();
    }
}
